package com.spring.hot;

import java.util.HashMap;

/**
 * map解决
 * @author Cizai
 * @since 2022/11/29 10:00
 * @see <a href="https://leetcode.cn/problems/two-sum/?favorite=2cktkvj">...</a>
 **/
public class 二数之和 {

    public int[] twoSum(int[] nums, int target) {

        int[] ints = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            // 如果存在相加等于target的话返回他俩
            if (map.containsKey(target-nums[i])){
                ints[0] = i;
                ints[1] = map.get(target - nums[i]);
            }else {
                map.put(nums[i], i);
            }
        }
        return ints;

    }
}
